<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class m_obat_data extends CI_Model {
	var $table = "obat";

	public function __construct(){
		parent::__construct();
	}	

	public function get_all(){
		$sql = "SELECT * FROM $this->table o 
		JOIN kelompok k ON o.KdKelompok = k.KdKelompok
		ORDER BY KdObat DESC
		";
		return $sql;
	}

	public function search($input){
		$filter = $input['filter'];
		$keyword = $input['keyword'];

		if($filter == 'by_code'){
			if(is_numeric($keyword)){
				$sql = "SELECT * FROM $this->table o
				JOIN kelompok k ON o.KdKelompok = k.KdKelompok
				WHERE KdObat = $keyword
				ORDER BY KdObat DESC
				";
			}else{
				set_flash('danger','Format keyword salah');
				redirect('obat_data', 'refresh');
			}
		}else if($filter == 'by_name'){
			$sql = "SELECT * FROM $this->table o 
			JOIN kelompok k ON o.KdKelompok = k.KdKelompok
			WHERE NmObat LIKE '%$keyword%'
			ORDER BY KdObat DESC
			";
		}else if($filter == 'by_info'){
			$sql = "SELECT * FROM $this->table o
			JOIN kelompok k ON o.KdKelompok = k.KdKelompok
			WHERE Informasi LIKE '%$keyword%'
			ORDER BY KdObat DESC
			";
		}
		return $sql;
	}

	public function store($input){
		$data = array(
			'NmObat'				=> $input['nama'],
			'KdKelompok'		=> $input['kelompok'],
			'MinDosis'			=> $input['mindosis'],
			'MaxDosis'			=> $input['maxdosis'],
			'Satuan'				=> $input['satuan'],
			'SatuanDosis'		=> $input['satuandosis'],
			'Informasi'			=> $input['informasi']
			);
		$do_save = $this->db->insert($this->table, $data);

		if($do_save){
			set_flash('success','Data obat berhasil ditambahkan');
			return true;
		}else{
			set_flash('danger','Data obat gagal ditambahkan');
			return false;
		}
	}

	public function get_by_id($id){
		$this->db->where('KdObat', $id);
		$query = $this->db->get($this->table);

		if($query->num_rows() > 0){
			$result = $query->row_array();
			$query->free_result();
		}else{
			$result = array();
		}

		return $result;
	}

	public function update($input){
		$data = array(
			'NmObat'				=> $input['nama'],
			'KdKelompok'		=> $input['kelompok'],
			'MinDosis'			=> $input['mindosis'],
			'MaxDosis'			=> $input['maxdosis'],
			'Satuan'				=> $input['satuan'],
			'SatuanDosis'		=> $input['satuandosis'],
			'Informasi'			=> $input['informasi']
			);

		$this->db->where('KdObat', $input['KdObat']);
		$do_update = $this->db->update($this->table, $data);

		if($do_update){
			set_flash('success','Data obat berhasil diperbaharui');
			return true;
		}else{
			set_flash('danger','Data obat gagal diperbaharui');
			return false;
		}
	}

	public function destroy($id){
		$this->db->where('KdObat', $id);
		$do_delete = $this->db->delete($this->table);

		if($do_delete){
			set_flash('success','Data obat berhasil dihapus');
			return true;
		}else{
			set_flash('danger','Data obat gagal dihapus');
			return false;
		}
	}

	public function export(){
		$this->db->join('kelompok', 'kelompok.KdKelompok = obat.KdKelompok', 'left');
		$query = $this->db->get($this->table);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	public function export_by($filter, $keyword){
		if($filter == 'by_code'){
			if(is_numeric($keyword)){
				$this->db->join('kelompok', 'kelompok.KdKelompok = obat.KdKelompok');
				$this->db->where('KdObat', $keyword);
				$this->db->order_by('KdObat', 'desc');
				$query = $this->db->get('obat');				

				if($query->num_rows() > 0) {
					$result = $query->result_array();
					$query->free_result();
				}else{
					$result = array();
				}
				return $result;
			}else{
				set_flash('danger','Format keyword salah');
				redirect('obat_data', 'refresh');
			}
		}else if($filter == 'by_name'){
			$this->db->join('kelompok', 'kelompok.KdKelompok = obat.KdKelompok');
			$where = "NmObat LIKE '%$keyword%'";
			$this->db->where($where);
			$this->db->order_by('KdObat', 'desc');
			$query = $this->db->get('obat');

			if($query->num_rows() > 0) {
				$result = $query->result_array();
				$query->free_result();
			}else{
				$result = array();
			}
			return $result;
		}else if($filter == 'by_info'){
			$this->db->join('kelompok', 'kelompok.KdKelompok = obat.KdKelompok');
			$where = "Informasi LIKE '%$keyword%'";
			$this->db->where($where);
			$this->db->order_by('KdObat', 'desc');
			$query = $this->db->get('obat');

			if($query->num_rows() > 0) {
				$result = $query->result_array();
				$query->free_result();
			}else{
				$result = array();
			}
			return $result;

			$sql = "SELECT * FROM $this->table o
			JOIN kelompok k ON o.KdKelompok = k.KdKelompok
			WHERE Informasi LIKE '%$keyword%'
			ORDER BY KdObat DESC
			";
		}
	}
}

/* End of file m_obat_data.php */
/* Location: ./application/models/m_obat_data.php */